package top.broncho.lottery.ui.image

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.annotation.MainThread
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.baidu.ocr.ui.camera.CameraActivity
import com.baidu.ocr.ui.camera.CameraActivity.CONTENT_TYPE_GENERAL
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine


inline fun AppCompatActivity.requestImage(
    path: String,
    crossinline requestBlock: Boolean.() -> Unit
) {
    PickImageManager.requestImage(this, path) { requestBlock.invoke(this) }
}

inline fun Fragment.requestImage(
    path: String,
    crossinline requestBlock: Boolean.() -> Unit
) {
    PickImageManager.requestImage(this, path) { requestBlock.invoke(this) }
}


suspend inline fun AppCompatActivity.requestImage(path: String) =
    suspendCoroutine<Boolean> { continuation ->
        PickImageManager.requestImage(this, path) {
            continuation.resume(this)
        }
    }

suspend inline fun Fragment.requestImage(path: String) =
    suspendCoroutine<Boolean> { continuation ->
        PickImageManager.requestImage(this, path) {
            continuation.resume(this)
        }
    }

/**
 * A simple [Fragment] subclass.
 */
class PickImageManager : Fragment() {
    private var resultCallback: (Boolean.() -> Unit)? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        retainInstance = true
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        onPermissionResult(requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
    }


    fun requestPermissions(resultCallback: Boolean.() -> Unit, path: String) {
        this.resultCallback = resultCallback
        startActivityForResult(Intent(context, CameraActivity::class.java).apply {
            putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, path)
            putExtra(CameraActivity.KEY_CONTENT_TYPE, CONTENT_TYPE_GENERAL)
        }, REQUEST_CODE)
    }

    private fun onPermissionResult(permissionResult: Boolean) {
        resultCallback?.invoke(permissionResult)

    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        resultCallback = null
    }

    override fun onDestroy() {
        super.onDestroy()
        resultCallback = null
    }

    companion object {

        private const val TAG = "PickImageManager"
        private const val REQUEST_CODE = 42


        @JvmStatic
        @MainThread
        fun requestImage(
            activity: AppCompatActivity,
            path: String,
            resultCallback: Boolean.() -> Unit
        ) {
            getImageFragment(activity.supportFragmentManager).apply {
                requestPermissions(resultCallback, path)
            }

        }

        @JvmStatic
        @MainThread
        fun requestImage(
            fragment: Fragment,
            path: String,
            resultCallback: Boolean.() -> Unit
        ) {
            getImageFragment(fragment.childFragmentManager).apply {
                requestPermissions(resultCallback, path)
            }
        }

        private fun getImageFragment(fragmentManager: FragmentManager): PickImageManager {
            var fragment = fragmentManager.findFragmentByTag(TAG)
            if (fragment != null) {
                fragment = fragment as PickImageManager
            } else {
                fragment = PickImageManager()
                fragmentManager.beginTransaction().add(fragment, TAG).commitNow()
            }
            return fragment
        }
    }

}